/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_ssap_server_proxy.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
int32_t SleSsapServerProxy::InnerTransact(
    uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
{
    if (code != SLE_SSAP_SERVER_NOTIFY_INDICATE) {
        SLE_LOGD("InnerTransact In");
    }
    auto remote = Remote();
    if (remote == nullptr) {
        SLE_LOGE("get remote failed, error code %{public}d", code);
        return OBJECT_NULL;
    }
    int32_t err = remote->SendRequest(code, data, reply, flags);
    if (err != NO_ERROR) {
        SLE_LOGE("send request failed, ipcErr=%{public}d code %{public}d", err, code);
    }
    return err;
}

SleErrorCode SleSsapServerProxy::RegisterSsapServer(const Uuid &uuid, uint8_t &serverId, const sptr<ISleSsapServerCallback> &callback)
{
    SLE_LOGD("RegisterSsapServer In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapServerProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&uuid), SLE_ERR_IPC_TRANS_FAILED, "write server uuid failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteRemoteObject(callback->AsObject()), SLE_ERR_IPC_TRANS_FAILED, "write server callback failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapServerInterfaceCode::SLE_SSAP_SERVER_REGISTER_SERVER, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    int32_t exception = reply.ReadInt32();
    SLE_ASSERT_AND_RETURN_RET_LOG(exception == NO_ERROR, exception, "register ssap server failed!(%d)", exception);
    serverId = reply.ReadUint8();
    return (SleErrorCode)exception;
}

SleErrorCode SleSsapServerProxy::UnregisterSsapServer(uint8_t serverId, const sptr<ISleSsapServerCallback> &callback)
{
    SLE_LOGD("UnregisterSsapServer In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapServerProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(serverId), SLE_ERR_IPC_TRANS_FAILED, "write server id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteRemoteObject(callback->AsObject()), SLE_ERR_IPC_TRANS_FAILED, "write server callback failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapServerInterfaceCode::SLE_SSAP_SERVER_UNREGISTER_SERVER, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapServerProxy::AddService(uint8_t serverId, const Uuid &uuid, bool isPrimary, uint16_t &handle)
{
    SLE_LOGD("AddService In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapServerProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(serverId), SLE_ERR_IPC_TRANS_FAILED, "write server id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&uuid), SLE_ERR_IPC_TRANS_FAILED, "write uuid failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteBool(isPrimary), SLE_ERR_IPC_TRANS_FAILED, "write primary failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapServerInterfaceCode::SLE_SSAP_SERVER_ADD_SERVICE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    int32_t exception = reply.ReadInt32();
    SLE_ASSERT_AND_RETURN_RET_LOG(exception == NO_ERROR, exception, "add service failed!(%d)", exception);
    handle = reply.ReadUint16();
    return (SleErrorCode)exception;
}

SleErrorCode SleSsapServerProxy::AddProperty(uint8_t serverId, uint16_t serviceHandle, const SsapServerPropertyParam &param, uint16_t &handle)
{
    SLE_LOGD("AddProperty In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapServerProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(serverId), SLE_ERR_IPC_TRANS_FAILED, "write server id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(serviceHandle), SLE_ERR_IPC_TRANS_FAILED, "write service handle failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&param), SLE_ERR_IPC_TRANS_FAILED, "write property param failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapServerInterfaceCode::SLE_SSAP_SERVER_ADD_PROPERTY, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    int32_t exception = reply.ReadInt32();
    SLE_ASSERT_AND_RETURN_RET_LOG(exception == NO_ERROR, exception, "add property failed!(%d)", exception);
    handle = reply.ReadUint16();
    return (SleErrorCode)exception;
}

SleErrorCode SleSsapServerProxy::AddDescriptor(uint8_t serverId, uint16_t serviceHandle, uint16_t propHandle, const SsapServerDescriptorParam &param)
{
    SLE_LOGD("AddDescriptor In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapServerProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(serverId), SLE_ERR_IPC_TRANS_FAILED, "write server id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(serviceHandle), SLE_ERR_IPC_TRANS_FAILED, "write service handle failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(propHandle), SLE_ERR_IPC_TRANS_FAILED, "write property handle failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&param), SLE_ERR_IPC_TRANS_FAILED, "write descriptor param failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapServerInterfaceCode::SLE_SSAP_SERVER_ADD_DESCRIPTOR, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapServerProxy::RemoveAllServices(uint8_t serverId)
{
    SLE_LOGD("RemoveAllServices In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapServerProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(serverId), SLE_ERR_IPC_TRANS_FAILED, "write server id failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapServerInterfaceCode::SLE_SSAP_SERVER_REMOVE_ALL_SERVICES, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapServerProxy::StartService(uint8_t serverId, uint16_t serviceHandle)
{
    SLE_LOGD("StartService In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapServerProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(serverId), SLE_ERR_IPC_TRANS_FAILED, "write server id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(serviceHandle), SLE_ERR_IPC_TRANS_FAILED, "write service handle failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapServerInterfaceCode::SLE_SSAP_SERVER_START_SERVICE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapServerProxy::SendResponse(uint8_t serverId, uint16_t connectId, const SsapServerSendRspParam &param)
{
    SLE_LOGD("SendResponse In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapServerProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(serverId), SLE_ERR_IPC_TRANS_FAILED, "write server id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(connectId), SLE_ERR_IPC_TRANS_FAILED, "write connect id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&param), SLE_ERR_IPC_TRANS_FAILED, "write response param failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapServerInterfaceCode::SLE_SSAP_SERVER_SEND_RESPONSE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapServerProxy::NotifyIndicate(uint8_t serverId, uint16_t connectId, const SsapServerNotifyParam &param)
{
#if 0
    SLE_LOGD("NotifyIndicate In");
#endif
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapServerProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(serverId), SLE_ERR_IPC_TRANS_FAILED, "write server id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(connectId), SLE_ERR_IPC_TRANS_FAILED, "write connect id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&param), SLE_ERR_IPC_TRANS_FAILED, "write notify indicate param failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapServerInterfaceCode::SLE_SSAP_SERVER_NOTIFY_INDICATE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapServerProxy::NotifyIndicateByUuid(uint8_t serverId, uint16_t connectId, const SsapServerNotyfyByUuidParam &param)
{
    SLE_LOGD("NotifyIndicateByUuid In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapServerProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(serverId), SLE_ERR_IPC_TRANS_FAILED, "write server id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(connectId), SLE_ERR_IPC_TRANS_FAILED, "write connect id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&param), SLE_ERR_IPC_TRANS_FAILED, "write notify indicate by uuid param failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapServerInterfaceCode::SLE_SSAP_SERVER_NOTIFY_INDICATE_BY_UUID, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleSsapServerProxy::SetServerMtuInfo(uint8_t serverId, uint32_t mtu, uint16_t version)
{
    SLE_LOGD("SetServerMtuInfo In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleSsapServerProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(serverId), SLE_ERR_IPC_TRANS_FAILED, "write server id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint32(mtu), SLE_ERR_IPC_TRANS_FAILED, "write mtu failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint16(version), SLE_ERR_IPC_TRANS_FAILED, "write version failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleSsapServerInterfaceCode::SLE_SSAP_SERVER_SET_MTU_INFO, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}
} // SleStandard
} // NearLink
} // OHOS